package com.health.viewscontrols;

import java.util.ArrayList;

import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.Main_admin_loginFragment;
import com.health.fragment.jtys.JTYSJD;
import com.health.tools.IDCardTool.IDCardConnectListener;
import com.health.viewscontrols.settings.MainActivity_Menu_Settings_JTYS;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;

/**
 * 主登录界面
 * 
 * @author nova
 *
 */
public class AddCustomer extends com.nova.BaseFragmentActivity
		implements OnClickListener, IDCardConnectListener {

	private ViewPager viewPager;
	private ArrayList<RadioButton> radioButtons;
	private ArrayList<Fragment> fragments;

	private JTYSJD tjFragement;
	private ImageView img_settings;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.mainactiviy_login);
		AddCustomer.this.setFinishOnTouchOutside(false);
		initView();
		addListener();
	}


	private void initView() {

		img_settings = (ImageView) findViewById(R.id.img_settings);
		img_settings.setOnClickListener(this);

		viewPager = (ViewPager) findViewById(R.id.viewpager);
		RadioButton main_tj = (RadioButton) findViewById(R.id.main_tj);
		RadioButton main_admin = (RadioButton) findViewById(R.id.main_admin);
		main_admin.setVisibility(View.GONE);
		radioButtons = new ArrayList<RadioButton>();
		radioButtons.add(main_tj);
		radioButtons.add(main_admin);

		main_tj.setOnClickListener(this);
		main_admin.setOnClickListener(this);

		tjFragement = new JTYSJD();
		Fragment adminFragment = new Main_admin_loginFragment();
		fragments = new ArrayList<Fragment>();
		fragments.add(tjFragement);
		fragments.add(adminFragment);

		FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
		viewPager.setAdapter(adapter);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (NovaApplication.idCardTool != null) {
			NovaApplication.idCardTool.removeListener();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (NovaApplication.idCardTool != null) {
			NovaApplication.idCardTool.setListener(this);
		}
	}

	private void addListener() {

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				radioButtons.get(position).setChecked(true);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.main_tj:
			viewPager.setCurrentItem(0);
			break;
		case R.id.main_admin:
			viewPager.setCurrentItem(1);
			break;
		case R.id.img_settings:
			Intent intent = new Intent(getApplicationContext(), MainActivity_Menu_Settings_JTYS.class);
			startActivity(intent);
			break;

		default:
			break;
		}

	}

	@Override
	public void onIDCardConnect() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onIDDataReceived(String data) {
		// TODO Auto-generated method stub
		tjFragement.setIdInfo(data);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		if (NovaApplication.bloothTool != null) {
//			NovaApplication.bloothTool.closeAndExit();
//		}
	}


}
